package com.xuan.temper.utils;

import com.xuan.temper.entity.Data;
import com.xuan.temper.entity.ExponeEntity;
import com.xuan.temper.entity.ExponentialTemperature;
import com.xuan.temper.entity.ReturnData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @创建人： king xuan
 * @创建时间：2020/9/6
 * @描述：
 */
public class CalculateExponentialTemperatureUtils {

    public static ExponentialTemperature CalculateExponentialTheTemperature(List<ExponentialTemperature> exponentialTemperatures,String endTime){

        //pe
        ArrayList<Double> peList = new ArrayList<>();
        //pb
        ArrayList<Double> pbList = new ArrayList<>();

        ArrayList<ExponeEntity> dateList = new ArrayList<>();

        for (ExponentialTemperature exponentialTemperature : exponentialTemperatures) {
            String stockCode = exponentialTemperature.getStockCode();
            Date date = exponentialTemperature.getTime();
            Double pe_ttm =exponentialTemperature.getPeJq();
            Double pb =exponentialTemperature.getPbJq();
            peList.add(pe_ttm);
            pbList.add(pb);
            ExponeEntity ExponeEntity = new ExponeEntity();
            ExponeEntity.setDate(date);
            ExponeEntity.setStockCode(stockCode);
            ExponeEntity.setPb(pb);
            ExponeEntity.setPe_ttm(pe_ttm);
            ExponeEntity.setName(exponentialTemperature.getName());
            dateList.add(ExponeEntity);
        }
        Collections.sort(peList);
        Collections.sort(pbList);
        //数组大小
        Double peListSize = Double.valueOf(peList.size());
        Double pbListSize = Double.valueOf(pbList.size());
        //间隔因数
        Double i = 1 / peListSize;
        for(int j=0;j<pbList.size();j++){
            double pbValue = pbList.get(j);
            double peValue = peList.get(j);
            double pbTemper = (j * i)*100;
            double peTemper = (j * i)*100;
            for (com.xuan.temper.entity.ExponeEntity ExponeEntity : dateList) {
                if (pbValue== ExponeEntity.getPb()){
                    ExponeEntity.setPbTemper(pbTemper);
                }
                if (peValue== ExponeEntity.getPe_ttm()){
                    ExponeEntity.setPeTemper(peTemper);
                }
            }
        }
        SimpleDateFormat sd=new SimpleDateFormat("yyyy年MM月dd日");
        ExponentialTemperature temperature = new ExponentialTemperature();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for (ExponeEntity ExponeEntity : dateList) {
            String returnEndeTime = format.format(ExponeEntity.getDate());
            if (returnEndeTime.equals(endTime)){
                temperature.setName(ExponeEntity.getName());
                temperature.setStockCode(ExponeEntity.getStockCode());
                temperature.setPbJq(ExponeEntity.getPb());
                temperature.setPbTemp(ExponeEntity.getPbTemper());
                temperature.setPeJq(ExponeEntity.getPe_ttm());
                temperature.setPeTemp(ExponeEntity.getPeTemper());
                temperature.setTime(ExponeEntity.getDate());
                Double currenTemp = (ExponeEntity.getPbTemper()+ ExponeEntity.getPeTemper())/2;
                temperature.setCurrenTemp(currenTemp);
                System.out.println("当前指数代码为: " + ExponeEntity.getStockCode());
                System.out.println("日期 = " + sd.format(ExponeEntity.getDate()));
                System.out.println("pe值 = " + ExponeEntity.getPe_ttm());
                System.out.println("pe温度 = " + ExponeEntity.getPeTemper());
                System.out.println("pb值 = " + ExponeEntity.getPb());
                System.out.println("pb温度 = " + ExponeEntity.getPbTemper());
                System.out.println("指数温度 = " + (ExponeEntity.getPbTemper()+ ExponeEntity.getPeTemper())/2);
                System.out.println("指数ROE = " +(ExponeEntity.getPbTemper()/ ExponeEntity.getPeTemper()) *100+"%");
                System.out.println("------------------------------");
                break;
            }
        }
       return temperature;
    }
}
